package com.jz.community.basecomm.base;

import android.net.Uri;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.github.piasy.biv.BigImageViewer;
import com.github.piasy.biv.view.BigImageView;
import com.gyf.barlibrary.ImmersionBar;
import com.jz.community.basecomm.mvp.BaseMvpPresenter;
import com.jz.community.basecomm.utils.GlideImageLoader;
import com.jz.community.basecommunity.R;
import com.jz.community.basecommunity.R2;

import java.io.File;
import java.util.ArrayList;

import butterknife.BindView;

public class ImageBrowserActivity extends BaseMvpActivity {

    @BindView(R2.id.browser_black_bg)
    RelativeLayout browserBlackBg;
    @BindView(R2.id.browser_pager)
    ViewPager browserPager;
    @BindView(R2.id.browser_num)
    TextView browserNum;

    public final static String Key_ImageList = "Key_ImageList";
    public final static String Key_CurrentPosition = "Key_CurrentPosition";
    public final static String Is_From_File = "isFromFile";


    private ArrayList<String> imageUrlList;
    private int currentPosition;
    private int isFromFile = -1;


    @Override
    protected int getContentView() {
        return R.layout.activity_image_browser;
    }

    @Override
    protected BaseMvpPresenter createPresenter() {
        return null;
    }

    @Override
    protected void initView() {
        ImmersionBar.with(this)
                .transparentStatusBar()  //透明状态栏，不写默认透明色
                .transparentNavigationBar()//透明导航栏，不写默认黑色(设置此方法，fullScreen()方法自动为true
                .init();
        BigImageViewer.initialize(GlideImageLoader.with(this));
        imageUrlList = getIntent().getStringArrayListExtra(Key_ImageList);
        currentPosition = getIntent().getIntExtra(Key_CurrentPosition, 1);
        isFromFile = getIntent().getIntExtra(Is_From_File, -1);
        browserNum.setText(String.valueOf((currentPosition + 1) + "/" + imageUrlList.size()));
        initViewPager();
    }


    private void initViewPager() {
        browserPager.setAdapter(new MyAdapter());
        browserPager.setCurrentItem(currentPosition);
        browserPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                browserNum.setText(String.valueOf((position + 1) + "/" + imageUrlList.size()));
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void finishBrowser() {
        browserNum.setVisibility(View.GONE);
        browserBlackBg.setAlpha(0);
        finish();
    }

    @Override
    public void onBackPressedSupport() {
        finishBrowser();
    }


    private class MyAdapter extends PagerAdapter {

        private LayoutInflater layoutInflater;

        public MyAdapter() {
            layoutInflater = LayoutInflater.from(mActivity);
        }

        @Override
        public int getCount() {
            return imageUrlList.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }

        @Override
        public Object instantiateItem(ViewGroup container, final int position) {
            View inflate = layoutInflater.inflate(R.layout.image_browser, container, false);
            BigImageView imageView = inflate.findViewById(R.id.item_browser_photo);
            if (isFromFile == -1) {
                imageView.showImage(Uri.parse(imageUrlList.get(position)));
            } else {
                imageView.showImage(Uri.fromFile(new File(imageUrlList.get(position))));
            }
            imageView.setOnClickListener(v -> finishBrowser());
            container.addView(inflate);
            return inflate;
        }
    }

}
